<?php

class Admin_HotelController extends Zend_Controller_Action
{
	protected $_em;
	protected $_repo;
	public function init()
	{
		$this->_em = Zend_Registry::get('doctrine');
		$this->_flash = $this->_helper->getHelper('FlashMessenger');
	}
    /************** HOTEL *************/
	public function indexAction() {
		if($this->getRequest()->isPost())
		{
			$country = $this->getRequest()->getParam("country");
			$region = $this->getRequest()->getParam("region");
			$city = $this->getRequest()->getParam("city");
			$name = $this->getRequest()->getParam("name");
			$hotelObj = new Admin_Model_DbTable_hotel();
			if($city != '0')
			{
				return $hotelObj->getBy($city,$name)->toArray();
			}else if($region != '0')
			{
				$cityObj = new Admin_Model_DbTable_city();
				$data = $cityObj->getByRegion($region);
				$result=array();
				foreach($data as $item)
				{
					$tmp = $hotelObj->getBy($item->id, $name);
					if($tmp->count()>0)
					{
						foreach ($tmp as $item)
						{
							array_push($result,$item->toArray());
						}
					}
				}
				print_r($result);
				$this->view->hotel=$result;
			}else if($country != '0')
			{
				$countryObj = new Admin_Model_DbTable_city();
				$data=$countryObj->getByCountry($country);
				foreach($data as $item)
				{
					$tmp = $hotelObj->getBy($item->id, $name);
					if($tmp->count()>0)
						$result[]= $tmp->toArray();
				}
				$this->view->hotel=$result;
			}else 
			{
				$hotelObj = new Admin_Model_DbTable_hotel();
			   $data=$hotelObj->getList();
			   $count = $data->count();
			   $cityObj = new Admin_Model_DbTable_city();
			   if($count > 0)
			   {
			   		$data=$data->toArray();
			   		for ($i=0;$i<$count;$i++)
			   		{
			   			
			   				$tmp = $cityObj->getCity($data[$i]["cityId"]);
			   				$data[$i]["cityId"]=$tmp["name_en"];
			   		}
			   		$this->view->hotel=$data;
			   }
			}
		}else 
		{
		   $hotelObj = new Admin_Model_DbTable_hotel();
		   $data=$hotelObj->getList();
		   $count = $data->count();
		   $cityObj = new Admin_Model_DbTable_city();
		   if($count > 0)
		   {
		   		$data=$data->toArray();
		   		for ($i=0;$i<$count;$i++)
		   		{
		   			
		   				$tmp = $cityObj->getCity($data[$i]["cityId"]);
		   				$data[$i]["cityId"]=$tmp["name_en"];
		   		}
		   		$this->view->hotel=$data;
		   }
		}
	   $form = new Admin_Form_search();
	    $countryObj = new Admin_Model_DbTable_country();
	   $data = $countryObj->getAllCountry();
	   $result = array("0"=>"All");
	   foreach ($data as $item)
	   {
	   		$result[$item->id]=$item->name_en;
	   }
	   $form->getElement("country")->addMultiOptions($result);
	   
	   $regionObj = new Admin_Model_DbTable_region();
	   $data = $regionObj->getallregion();
	   $result=array("0"=>"All");
	   foreach ($data as $item)
	   {
	   		$result[$item->id]= $item->name_en;
	   }
	   $form->getElement("region")->addMultiOptions($result);
	   $cityObj = new Admin_Model_DbTable_city();
	   $data = $cityObj->getAllCity();
	   $result=array("0"=>"All");
	   foreach($data as $item)
	   {
	  	$result[$item->id]=$item->name_en;
	   }
	   $form->getElement("city")->addMultiOptions($result);
	   $form->getElement("submit")->setLabel("Search");
	   $this->view->search=$form;
	   $this->view->region = $regionObj->getallregion();
	   $this->view->city = $cityObj->getAllCity();
	}
	public function delhotelAction()
	{
		$id = $this->getRequest()->getParam("id");
		$hotelObj = new Admin_Model_DbTable_hotel();
		if($hotelObj->delhotel($id))
		{
			$this->_redirect("/admin/hotel/index");
		}
	}
	public function edithotelAction()
	{
		$id= $this->getRequest()->getParam("id");
		if($this->getRequest()->isPost() && isset($id))
		{
			$name= $this->getRequest()->getParam("name");
			$address=$this->getRequest()->getParam("address");
			$website= $this->getRequest()->getParam("website");
			$priority=$this->getRequest()->getParam("priority");
			$rate=$this->getRequest()->getParam("rate");
			$city=$this->getRequest()->getParam("city");
			$des_vi=$this->getRequest()->getParam("des_vi");
			$des_en=$this->getRequest()->getParam("des_en");
			$des_ja=$this->getRequest()->getParam("des_ja");
			$des_cn=$this->getRequest()->getParam("des_cn");
			$des_fr=$this->getRequest()->getParam("des_fr");
			$accomodation= $this->getRequest()->getParam("accomodation");
			$service=$this->getRequest()->getParam("service");
			$status = $this->getRequest()->getParam("status");
			$meeting = $this->getRequest()->getParam("meeting");
			$re_accomodation="";
			foreach($accomodation as $item)
			{
				$re_accomodation=$re_accomodation.$item.'_';
			}
			$re_accomodation=substr($re_accomodation, 0,-1);
			$re_service="";
			foreach ($service as $item)
			{
				$re_service=$re_service.$item.'_';
			}
			$re_service=substr($re_service,0,-1);
			$hotelObj= new Admin_Model_DbTable_hotel();
			if($hotelObj->updateHotel($id, $name, $address, $website, $priority, $rate, $city, $des_vi, $des_en, $des_ja, $des_cn, $des_fr, $re_accomodation, $re_service, NULL, $meeting))
			{
				$this->_redirect("/admin/hotel/index");
			}else echo "0";
			return;
		}
		if(isset($id))
		{
			$form = new Admin_Form_Hotel();
			$hotelObj = new Admin_Model_DbTable_hotel();
			$datahotel = $hotelObj->getHotel($id);
			$form->getElement("name")->setValue($datahotel->name);
			$form->getElement("address")->setValue($datahotel->address);
			$form->getElement("website")->setValue($datahotel->website);
			$form->getElement("priority")->setValue($datahotel->priority);
			$form->getElement("rate")->setValue($datahotel->rate);
			$form->getElement("des_vi")->setValue($datahotel->desc_vi);
			$form->getElement("des_en")->setValue($datahotel->desc_en);
			$form->getElement("des_ja")->setValue($datahotel->desc_ja);
			$form->getElement("des_cn")->setValue($datahotel->desc_cn);
			$form->getElement("des_fr")->setValue($datahotel->desc_fr);
			$cityObj = new Admin_Model_DbTable_city();
			$city=$cityObj->getCity($datahotel->cityId);
			$countryObj = new Admin_Model_DbTable_country();
			$data = $countryObj->getAllCountry();
			$result=array();
			foreach($data as $item)
			{
				$result[$item->id]=$item->name_en;
			}
			$form->getElement("country")->addMultiOptions($result);
			$form->getElement("country")->setValue($city["countryId"]);
			$listCity=$countryObj->getListCity($city["countryId"]);
			$result=array();
			foreach($listCity as $item)
			{
				$result[$item->id]=$item->name_en;
			}
			$form->getElement("city")->addMultiOptions($result);
			$form->getElement("city")->setValue($city["id"]);
			$accomodation = new Admin_Model_DbTable_accomodation();
			$data=$accomodation->getAll();
			$result=array();
			foreach($data as $item)
			{
				$result[$item->id]=$item->name_en;
			}
			$form->getElement("accomodation")->addMultiOptions($result);
			$form->getElement("accomodation")->setValue(explode("_",$datahotel->accomodation));
			$service =  new Admin_Model_DbTable_service();
			$data=$service->getAll();
			$result=array();
			foreach($data as $item)
			{
				$result[$item->id]=$item->name_en;
			}
			$form->getElement("service")->addMultiOptions($result);
			$form->getElement("service")->setValue(explode("_", $datahotel->service));
			$form->getElement("add")->setLabel("Update");
			$this->view->hotel=$form;	
			}
	}	
	public function addhotelAction()
	{
		if($this->getRequest()->isPost())
		{
			$name= $this->getRequest()->getParam("name");
			$address=$this->getRequest()->getParam("address");
			$website= $this->getRequest()->getParam("website");
			$priority=$this->getRequest()->getParam("priority");
			$rate=$this->getRequest()->getParam("rate");
			$city=$this->getRequest()->getParam("city");
			$des_vi=$this->getRequest()->getParam("des_vi");
			$des_en=$this->getRequest()->getParam("des_en");
			$des_ja=$this->getRequest()->getParam("des_ja");
			$des_cn=$this->getRequest()->getParam("des_cn");
			$des_fr=$this->getRequest()->getParam("des_fr");
			$accomodation= $this->getRequest()->getParam("accomodation");
			$service=$this->getRequest()->getParam("service");
			$status = $this->getRequest()->getParam("status");
			$meeting = $this->getRequest()->getParam("meeting");
			$re_accomodation="";
			
			foreach($accomodation as $item)
			{
				$re_accomodation=$re_accomodation.$item.'_';
			}
			$re_accomodation=substr($re_accomodation, 0,-1);
			$re_service="";
			foreach ($service as $item)
			{
				$re_service=$re_service.$item.'_';
			}
			$re_service=substr($re_service,0,-1);
			$hotelObj= new Admin_Model_DbTable_hotel();
			if($hotelObj->addHotel($name, $address, $website, $priority, $rate, $city, $des_vi, $des_en, $des_ja, $des_cn, $des_fr, $re_accomodation, $re_service, NULL, $meeting))
			{
				$this->_redirect("/admin/hotel/index");
			}else echo "0";
			return;
		}
		$form = new Admin_Form_Hotel();
		$countryObject= new Admin_Model_DbTable_country();
		$data = $countryObject->getAllCountry();
		$result=array();
		foreach($data as $item)
		{
			$result[$item->id]=$item->name_en;
		}
		$form->getElement("country")->addMultiOptions($result);
		$accomodation = new Admin_Model_DbTable_accomodation();
		$data=$accomodation->getAll();
		$result=array();
		foreach($data as $item)
		{
			$result[$item->id]=$item->name_en;
		}
		$form->getElement("accomodation")->addMultiOptions($result);
		$service =  new Admin_Model_DbTable_service();
		$data=$service->getAll();
		$result=array();
		foreach($data as $item)
		{
			$result[$item->id]=$item->name_en;
		}
		$form->getElement("service")->addMultiOptions($result);
		$this->view->form = $form;
		
	}
	
	/************** CHANGE PRIORITY *************/
	public function changepriorityAction()
	{
		$this->_helper->layout->disableLayout();
		$id = $this->getRequest()->getParam("id");
		$name = $this->getRequest()->getParam("name");
		$priority = $this->getRequest()->getParam("priority");
		$db = "Admin_Model_DbTable_".$name;
		$db_tb = new $db();
		$get = "get".$name;
		$detail = $db_tb->$get($id);
		$db_tb->changepriority($id,$priority);
	
	}
}